package com.uphone.kingmall.utils;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.blankj.utilcode.util.KeyboardUtils;
import com.blankj.utilcode.util.LogUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.china.zhuoyue.R;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.app.MyApplication;
import com.uphone.kingmall.base.BaseBean;
import com.uphone.kingmall.listener.NetListCallBack;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.login.LoginActivity;
import com.uphone.kingmall.utils.intent.IntentUtils;
import com.uphone.kingmall.utils.navigationbar.DefaultNavigationBar;
import com.uphone.kingmall.view.dialog.OnDialogViewClickListener;
import com.uphone.kingmall.view.dialog.RadishDialog;

import java.util.ArrayList;
import java.util.List;


/**
 * <pre>
 *     author : radish
 *     e-mail : 15703379121@163.com
 *     time   : 2019/3/16
 *     desc   :
 * </pre>
 */
public class CommonUtil {


    public static String getStr(String str) {
        if (TextUtils.isEmpty(str)) {
            return "";
        }
        return str;
    }

    public static <T> List<T> getList(List<T> list, int count) {
        List<T> tempList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            if (i >= count) {
                break;
            }
            tempList.add(list.get(i));
        }
        return tempList;
    }

    public static void startIntent(Context context, Class clazz) {
        IntentUtils.getInstance().with(context, clazz).start();
    }

    public static void startIntent(Context context, Class clazz, int id) {

        IntentUtils.getInstance().with(context, clazz).putInt(ConstansUtils.ID, id).start();
    }


    public static void startIntentType(Context context, Class clazz, int type) {

        IntentUtils.getInstance().with(context, clazz).putInt(ConstansUtils.TYPE, type).start();
    }

    public static boolean checkViewEmpty(TextView tv) {
        return checkViewEmpty(null, tv);
    }

    public static boolean checkViewEmpty(String[] msgs, TextView... tvs) {
        for (int i = 0; i < tvs.length; i++) {
            String msg = null;
            if (msgs != null && msgs.length > i) {
                msg = msgs[i];
            }
            if (checkViewEmpty(msg, tvs[i])) {
                return true;
            }
        }
        return false;
    }

    public static boolean checkViewEmpty(TextView... tvs) {
        return checkViewEmpty(null, tvs);
    }

    public static boolean checkViewEmpty(String msg, TextView tv) {
        if (tv != null) {
            if (!TextUtils.isEmpty(tv.getText().toString().trim())) {
                return false;
            }
            if (!TextUtils.isEmpty(msg)) {
                ToastUtils.showShortToast(MyApplication.mContext, msg);
            }
            return true;
        }
        if (!TextUtils.isEmpty(msg)) {
            ToastUtils.showShortToast(MyApplication.mContext, msg);
        }
        return true;
    }

    public static void showAlterDialog(Context context, String msg, final OnAlterDialogCallBack callBack) {
        new RadishDialog.Builder(context).setView(R.layout.kingmall_dialog_alter)
                .setText(R.id.dialog_message, "" + msg)
                .setClick(R.id.dialog_submit, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(final RadishDialog dialog, View view) {
                        if (callBack != null) {
                            callBack.callBack(dialog, view);
                        }
                        dialog.dismiss();
                    }
                })
                .setClick(R.id.dialog_cancel, new OnDialogViewClickListener() {
                    @Override
                    public void onClick(RadishDialog dialog, View view) {
                        dialog.dismiss();
                    }
                }).show();
    }


    public interface OnAlterDialogCallBack {
        void callBack(RadishDialog dialog, View view);
    }

    /**
     * 加载网络列表数据
     *
     * @param adapter
     * @param list
     * @param url
     * @param params
     * @param callBack
     * @param <T>
     */
    public static <B extends BaseBean, T extends BaseBean.DataBean> void netRv(final B beans, final BaseQuickAdapter adapter, final List<T> list, final String url, HttpParams params, final int page, final NetListCallBack<T> callBack) {
        OkGoUtils.normalRequest(url, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                B bean = (B) GsonUtils.getGson().fromJson(body, beans.getClass());
                LogUtils.e("page:" + page + "bean.code:" + bean.getCode());
                if (bean.getCode() == 0) {
                    if (page == 1 || page == -1) {
                        list.clear();
                    }
                    if (bean.getData() != null && bean.getData().size() > 0) {
                        list.addAll(bean.getData());
                    } else if (page != 1 && page != -1) {
                        ToastUtils.showShortToast(MyApplication.mContext, "暂无更多数据");
                    }
                    if (callBack != null) {
                        callBack.callBack(list);
                    }
                    if (adapter != null) {
                        adapter.setNewData(list);
                    }
                }
            }

            @Override
            public void onError(Response<String> response) {
                LogUtils.e("网络错误" + url + "：" + response.body());
            }
        });
    }

    /**
     * 验证是否登录
     *
     * @return
     */
    public static boolean checkLogin() {
        if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
            startIntent(MyApplication.mContext, LoginActivity.class);
            return false;
        }
        return true;
    }

    public static DefaultNavigationBar initTitle(Activity activity, String title, ViewGroup parent) {
        return new DefaultNavigationBar.Builder(activity, parent).setTitle(title).builder();
    }


    public static void initTitle(Activity activity, String title, String right, ViewGroup parent, View.OnClickListener listener) {
        new DefaultNavigationBar.Builder(activity, parent).setTitle(title).setRightTitle(right).setRightClick(listener).builder();
    }


    public static void submitNetAndFinish(final Activity activity, String url, HttpParams params, final String msg) {
        OkGoUtils.progressRequest(url, activity, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    KeyboardUtils.hideSoftInput(activity);
                    ToastUtils.showShortToast(activity, msg + "");
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            activity.finish();
                        }
                    }, 500);
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

    public static void submitNet(final Context context, String url, HttpParams params, final String msg) {
        OkGoUtils.progressRequest(url, context, params, new onNormalRequestListener() {
            @Override
            public void onSuccess(String response, int code) {
                if (code == 0) {
                    ToastUtils.showShortToast(context, msg + "");
                }
            }

            @Override
            public void onError(Response<String> response) {

            }
        });
    }

//    public static void loginRY() {
//        if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
//            return;
//        }
//        OkGoUtils.normalRequest(MyUrl.chatToken, null, new onNormalRequestListener() {
//            @Override
//            public void onSuccess(String body, int code) {
//                if (code == 0) {
//                    try {
//                        JSONObject jsonObject = new JSONObject(body);
//                        final String rongyunToken = jsonObject.getString("rongyunToken");
//                        LogUtils.e("rougyunToken:" + rongyunToken);
//                        RongIM.connect(rongyunToken, new RongIMClient.ConnectCallback() {
//                            @Override
//                            public void onTokenIncorrect() {
//                            }
//
//                            @Override
//                            public void onSuccess(String s) {
//                                SharedPreferencesHelper.saveRyToken(rongyunToken);
//                            }
//
//                            @Override
//                            public void onError(RongIMClient.ErrorCode e) {
//                            }
//                        });
//                    } catch (Exception e) {
//                        e.printStackTrace();
//                    }
//                }
//            }
//
//            @Override
//            public void onError(Response<String> response) {
//
//            }
//        });
//    }
}
